package javapoker.view;

/**
 **********************************************************************
 * Copyright (c) 2013 Kernull.com
 * 
 * All rights reserved. No warranty, explicit or implicit, provided.
 * 
 * @author 	Christopher Aprea <caprea@kernull.com>
 * @date 	Oct 7, 2013
 * @project javapoker
 * 
 **********************************************************************
 */


import java.awt.Dimension;
import java.util.Collections;
import java.util.Vector;

import javax.swing.JFrame;

import javapoker.Card;
import javapoker.Deck;
import javapoker.Hand;
import javapoker.Player;
import javapoker.Pot;

/**
 * The Class Dealer.
 */
public class DummyDealer 
{
	int np = 3;
	//Create the players
	private Vector<Player> players;
	private long ante=20;
	private Deck deck;
	private Hand community;
	private Pot pot;
	private JFrame _frame = null;
	
	public static void main(String args[])
	{
		DummyDealer dealer = new DummyDealer();
		
		dealer.createTable();
		dealer.playGame();
	}
	
	
	public void playGame()
	{


		for (int x=0; x< 500; x++)//play X hands
		{
			resetRound();
			
			evaluateHands();
		}
		
		for ( Player p : players)
		{
			System.out.println(p.getPlayerName() + ": " + p.getCredits());
		}

		
	}
	
	public void createTable()
	{
		deck = new Deck();
		players = new Vector<Player>(0);
		pot = new Pot(ante);
		community = new Hand();
				
		for (int i=0; i < np; i++)
		{
			Player challenger = new Player("PL" + i, 1000);

			players.add(challenger);			
		}
		
		
		
	}
	
	public void resetRound()
	{
		deck.reset();
		deck.shuffle();
		pot.reset();
		community.clear();
		
		//Setup the board
		//For each player
		for (Player p : players)
		{
			//Clear previous hand
			p.getHand().clear();
			
			//Ante up
			if ( p.getCredits() >= ante)
			{
				pot.contribute(p, ante);
				
				//Deal to each player
				p.drawX(2, deck);
			}
		}
		
		//Create the common pool
		community.addCards(deck.drawX(3));
		
	
	}
	
	
	public void evaluateHands()
	{
		for ( Player p : players)
		{
			//dont give any cards to the inactive players
			if ( pot.isPlaying(p) )
			{
				Hand h = p.getHand();
				h.addCards(community);	
			}
			else
			{
				
			}
		}
		Collections.sort(players);
		
		//Winner!
		Player winner = players.get(players.size()-1);
		
		pot.payWinner(winner);
		
		System.out.println(winner.getPlayerName() + " won with " + winner.getHand().abbreviatedString() + " com " + community.abbreviatedString() + " CR:" + winner.getCredits());
		System.out.println("\t " + players.get(players.size()-2).getHand().abbreviatedString());
		
		
	}
	

}
